package com.abp.controller;

import com.abp.entity.*;
import com.abp.qpage.QPage;
import com.abp.service.BookService;
import com.abp.service.IBookLendService;
import com.abp.service.IBookLibaryService;
import com.abp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/admin/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public List<Book> getAllBooks() {
        return bookService.list();
    }

    @GetMapping("/{id}")
    public Book getBookById(@PathVariable int id) {
        return bookService.getById(id);
    }

    @PostMapping
    public boolean addBook(@RequestBody Book book) {
        return bookService.save(book);
    }

    @PutMapping("/{id}")
    public boolean updateBook(@PathVariable int id, @RequestBody Book book) {
        book.setBookId(id);
        return bookService.updateById(book);
    }

    @DeleteMapping("/{id}")
    public boolean deleteBook(@PathVariable int id) {
        return bookService.removeById(id);
    }



    /**
     * 路由地址
     */
    public String prefix = "/view/admin/book";


    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, Book obj) {
        return Result.reOk("查询成功",this.bookService.getPageList(qPage,obj));
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Book obj) {
        return this.bookService.insert(obj);
    }

    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){
        return prefix+"/edit";
    }

    /**
     * 根据ID获取信息
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        Book obj=this.bookService.getById(id);
        return Result.reOk("查询成功",obj);
    }

    /**
     * 编辑保存
     * @return
     */
    @RequestMapping("/updates")
    @ResponseBody
    public Result updates(Book obj) {
        return this.bookService.updates(obj);
    }


    /**
     * 删除保存
     * @return
     */
    @RequestMapping("/deleteId")
    @ResponseBody
    public Result deleteId(int id) {
        boolean isOk=this.bookService.removeById(id);
        if(isOk){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }

    @Autowired
    private IBookLendService bookLendService;

    /**
     * 借出图书
     * @return
     */
    @RequestMapping("/lendBook")
    @ResponseBody
    public Result lendBook(int id, HttpServletRequest request) {
        Book book=bookService.getById(id);
        if(book.getStatus()==2){
            return Result.reError("当前图书已被借出，不可重复租借");
        }
        book.setStatus(2);
        this.bookService.updateById(book);
        User user=(User)request.getSession().getAttribute("user");
        BookLend bookLend1=new BookLend();
        bookLend1.setUserId(user.getUserId());
        bookLend1.setBookId(book.getBookId());
        bookLend1.setLendStatus(1);
        bookLend1.setLendTime(new Date());
        this.bookLendService.save(bookLend1);
        return Result.reOk("借出成功");
    }


}
